package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 根据用户的id查询推荐的小视频
     */
    @Override
    public PageBeanVo findVideoByUserId(Integer pageNumber, Integer pageSize, Long loginUserId) {
        //1.查询推荐的视频
        int startIndex = (pageNumber - 1) - pageSize;
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        )
                .with(Sort.by(Sort.Order.desc("score"), Sort.Order.desc("date")))//排序
                .skip(startIndex).limit(pageSize);
        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);

        //如果没有找到推荐  就构建几条假数据

        List<Video> videoList = new ArrayList<>();
        //2.根据推荐的视频id   查询视频的详情信息
        if (CollectionUtil.isNotEmpty(recommendVideos)) {
            for (RecommendVideo recommendVideo : recommendVideos) {
                //查询video对象
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                videoList.add(video);
            }
        }
        long counts = mongoTemplate.count(query, RecommendVideo.class);
        return new PageBeanVo(pageNumber,pageSize,counts,videoList);
    }

    @Autowired
    private IdService idService;
    /**
     * 保存视频
     * @param video
     * @return
     */
    @Override
    public ObjectId saveVideo(Video video) {
        //1.保存视频列表
        video.setCreated(System.currentTimeMillis());//时间
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID));//生成大数据使用id数据
        mongoTemplate.save(video);
        //2.保存到推荐表  ->可要可不要
        if (video != null) {
            RecommendVideo recommendVideo = new RecommendVideo();
            recommendVideo.setDate(System.currentTimeMillis());
            recommendVideo.setVid(video.getVid());
            recommendVideo.setUserId(video.getUserId());
            recommendVideo.setVideoId(video.getId());
            recommendVideo.setScore(RandomUtil.randomDouble(60, 90));
            mongoTemplate.save(recommendVideo);
        }
        return video.getId();
    }

    /**
     * 根据视频id查询视频
     * @param videoId
     * @return
     */
    @Override
    public Video findByVideoId(String videoId) {
        Video video = mongoTemplate.findById(new ObjectId(videoId), Video.class);
        return video;
    }

    /**
     * 分页查询视屏下的评论列表
     * @param pageNum
     * @param pageSize
     * @param videoId
     * @return
     */
    @Override
    public PageBeanVo findVideoComment(Integer pageNum, Integer pageSize, String videoId) {
        int startIndex = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(videoId)).and("commentType").is(5)
        )
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //查询记录总数
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    /**
     * 分页查询视频列表
     */
    @Override
    public PageBeanVo findVideoByUid(Integer pageNumber, Integer pageSize, Long uid) {
        //1.查询推荐的视频
        int startIndex = (pageNumber - 1) * pageSize;
        Query query = new Query(
                Criteria.where("userId").is(uid)
        )
                .with(Sort.by(Sort.Order.desc("date")))
                .skip(startIndex).limit(pageSize);

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);
        return new PageBeanVo(pageNumber, pageSize, count, videoList);
    }
}
